#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/6/12 12:55
# @Author  : TangZhongZheng
# @File    : insert_data.py
# @Description :

import warnings
from math import nan

import pandas as pd
from pandas._libs import NaTType
from sqlalchemy.future import create_engine
from sqlalchemy.orm import sessionmaker

from config import *
from data01.成员.model.member_info import MemberInfo

warnings.filterwarnings("ignore")

pd.options.mode.chained_assignment = None

engine = create_engine("mysql+pymysql://" + user + ":" + password + "@" + host + ":" + port + "/" + table_name)
Session = sessionmaker(bind=engine)
session = Session()


def isEmpty(data):
    if data == '' or data is None or type(data) == NaTType or data != data:
        return None
    return data


def pop_info(obj_dict):
    """
    删除不需要的字段信息
    :param obj_dict:
    :return:
    """
    obj_dict.pop(memberid)
    obj_dict.pop(member_link)
    obj_dict.pop(member_photo)
    obj_dict.pop(data_source)


def insert_data(data: pd.DataFrame, file):
    """
    插入数据
    :param data:
    :param file:
    :return:
    """
    with open(logs + "\\" + file + ".log", mode='w', encoding='utf-8') as f:
        try:
            for index, row in data.iterrows():
                obj_dict = {}
                for name, val in row.items():
                    obj_dict[name] = isEmpty(val)
                pop_info(obj_dict)
                obj = MemberInfo(**obj_dict)
                session.add(obj)
                session.commit()
        except Exception as e:
            print(e)
            session.rollback()
            info = (int(index.__str__()) + 2).__str__() + " " + str(e)
            f.write(info + "\n")
